package com.leo.foodmaster.admin.recipe.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.util.WebUtils;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import springfox.documentation.annotations.ApiIgnore;
import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.leo.foodmaster.admin.recipe.dto.RecipeTypeDTO;
import com.leo.foodmaster.admin.recipe.entity.RecipeTypeEntity;
import com.leo.foodmaster.admin.recipe.service.IRecipeTypeService;
import javax.annotation.Resource;


/**
 * 菜品类型
 *
 * @author Leo
 * @since 1.0.0 2021-10-29
 */
@RestController
@RequestMapping("/recipeType")
@Api(tags = "菜品类型")
@Slf4j
public class RecipeTypeController {
    @Resource
    private IRecipeTypeService irecipeTypeService;


    @GetMapping("/list")
    @ApiOperation("条件分页查询")
    public Result<IPage<RecipeTypeEntity>> page(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "菜品类型名称") @RequestParam(required = false) String name
    ){
        //单表查询
    	IPage<RecipeTypeEntity> pageList = irecipeTypeService.pageList(new Page<>(pageNum, pageSize),name);
        return Result.success(pageList);
    }

    @GetMapping("/listAll")
    @ApiOperation("查询公司菜品类型列表")
    public Result<List<RecipeTypeEntity>> listALl(){
        Long organId = WebUtils.getUser().getOrganId();
        List<RecipeTypeEntity> list = irecipeTypeService.list(Wrappers.lambdaQuery(RecipeTypeEntity.class)
                    .eq(RecipeTypeEntity::getOrganId, organId));
        return Result.success(list);
    }


    @GetMapping
    @ApiOperation("信息")
    public Result<RecipeTypeDTO> get(@RequestParam Long id,@ApiIgnore RecipeTypeDTO recipeTypeDTO){
    	RecipeTypeEntity byId = irecipeTypeService.getById(id);
    	if (BeanUtil.isEmpty(byId)) {
    		return Result.success();
		}
    	BeanUtil.copyProperties(byId, recipeTypeDTO);
    	return Result.success(recipeTypeDTO);
    }

    @PostMapping
    @ApiOperation("新增菜品分类")
    public Result<Boolean> save(@RequestBody RecipeTypeDTO dto){
        RecipeTypeEntity entity = new RecipeTypeEntity();
        BeanUtil.copyProperties(dto,entity);
        entity.setOrganId(WebUtils.getUser().getOrganId());
        Boolean save = irecipeTypeService.save(entity);
        return  Result.success(save);
    }


    @PutMapping
    @ApiOperation("修改")
    public Result<Boolean> update(@RequestBody RecipeTypeDTO dto){
        RecipeTypeEntity entity = new RecipeTypeEntity();
        BeanUtil.copyProperties(dto,entity);
        boolean updateById = irecipeTypeService.updateById(entity);
        return  Result.success(updateById);
    }


    @DeleteMapping("/{ids}")
    @ApiOperation("删除")
    public Result<Object> delete(@ApiParam("菜品类型ids") @PathVariable String ids){
        String[] idArray = ids.split(StrUtil.COMMA);
        List<Long> idList = new ArrayList<>();
        for (String id : idArray) {
            idList.add(Long.parseLong(id));
        }
        return irecipeTypeService.deleteBatchIds(idList);
    }

}
